package swings.labels;

import swings.buttons.CommonButton;
import swings.others.CommonJTextField;
import swings.panels.CommonPanel;
import swings.panels.WorkPanel;
import utils.CommonUtils;
import utils.ImageUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class ParamLabel extends JLabel {

    public CommonJTextField keyField = new CommonJTextField(25);
    public CommonJTextField valueField = new CommonJTextField(25);
    public CommonJTextField descriptionField = new CommonJTextField(25);
    private CommonButton yes;
    private CommonButton no;
    public CommonButton operate;
    public int symbol;
    public Boolean isChosen = true;
    public String key;
    public String value;
    public String description;
    public CommonPanel commonPanel;

    public ParamLabel(int y, int symbol, String key, String value, String description, CommonPanel commonPanel) throws IOException {
        this.symbol = symbol;
        this.key = key;
        this.value = value;
        this.description = description;
        this.commonPanel = commonPanel;
        setLabel(y);
    }

    public ParamLabel(int y, int symbol, String key, String description, CommonPanel commonPanel) throws IOException {
        this.symbol = symbol;
        this.key = key;
        this.description = description;
        this.commonPanel = commonPanel;
        setLabelWithBtn(y);
    }

    public ParamLabel(int y, int symbol, String key, String value) throws IOException {
        this.symbol = symbol;
        this.key = key;
        this.value = value;
        setLabelRep(y);
    }

    private void setLabel(int y) throws IOException {

        Font font = CommonUtils.getFont();
        int width = WorkPanel.width-90;
        this.setIcon(ImageUtils.common);
        this.setVerticalTextPosition(JLabel.CENTER);
        this.setHorizontalTextPosition(JLabel.CENTER);
        this.setFont(font);
        this.setForeground(Color.WHITE);
        this.setBounds(5,y,WorkPanel.width-20,30);
        keyField.setFont(CommonUtils.getFont());
        keyField.setForeground(Color.WHITE);
        keyField.setBounds(40,1,width/3-10,28);
        keyField.setOpaque(false);
        keyField.setText(key);
        valueField.setFont(CommonUtils.getFont());
        valueField.setForeground(Color.WHITE);
        valueField.setBounds(40 + width/3,1,width/3-10,28);
        valueField.setOpaque(false);
        valueField.setText(value);
        descriptionField.setFont(CommonUtils.getFont());
        descriptionField.setForeground(Color.WHITE);
        descriptionField.setBounds(40 + width*2/3,1,width/3-10,28);
        descriptionField.setOpaque(false);
        descriptionField.setText(description);
        //初始化选择和删除按钮
        yes = new CommonButton(ImageUtils.yes,5,5,20,20);
        no = new CommonButton(ImageUtils.no,43 + width,5,20,20);
        no.addActionListener(new deleteParam());
        yes.addActionListener(new statusListener());
        yes.setHoldListener();

        this.add(keyField);
        this.add(valueField);
        this.add(descriptionField);
        this.add(new BarLabel(32,0));
        this.add(new BarLabel(32 + width/3,0));
        this.add(new BarLabel(32 + width*2/3,0));
        this.add(new BarLabel(32 + width,0));
        this.add(yes);
        this.add(no);
//        addIsSkin();
//        this.add(bg);

    }

    private void setLabelWithBtn(int y) throws IOException {

        Font font = CommonUtils.getFont();
        int width = WorkPanel.width-90;
        this.setIcon(ImageUtils.common);
        this.setVerticalTextPosition(JLabel.CENTER);
        this.setHorizontalTextPosition(JLabel.CENTER);
        this.setFont(font);
        this.setForeground(Color.WHITE);
        this.setBounds(5,y,WorkPanel.width-20,30);
        keyField.setFont(CommonUtils.getFont());
        keyField.setForeground(Color.WHITE);
        keyField.setBounds(40,1,width/3-10,28);
        keyField.setOpaque(false);
        keyField.setText(key);
        valueField.setFont(CommonUtils.getFont());
        valueField.setForeground(Color.WHITE);
        valueField.setBounds(40 + width/3,1,width/3-10,28);
        valueField.setOpaque(false);
        valueField.setText(value);
        operate = new CommonButton(ImageUtils.operateImg,width*2/3,0,30,30);
        descriptionField.setFont(CommonUtils.getFont());
        descriptionField.setForeground(Color.WHITE);
        descriptionField.setBounds(40 + width*2/3,1,width/3-10,28);
        descriptionField.setOpaque(false);
        descriptionField.setText(description);
        //初始化选择和删除按钮
        yes = new CommonButton(ImageUtils.yes,5,5,20,20);
        no = new CommonButton(ImageUtils.no,43 + width,5,20,20);
        no.addActionListener(new deleteParam());
        yes.addActionListener(new statusListener());
        yes.setHoldListener();

        this.add(keyField);
        this.add(operate);
        this.add(valueField);
        this.add(descriptionField);
        this.add(new BarLabel(32,0));
        this.add(new BarLabel(32 + width/3,0));
        this.add(new BarLabel(32 + width*2/3,0));
        this.add(new BarLabel(32 + width,0));
        this.add(yes);
        this.add(no);
//        addIsSkin();
//        this.add(bg);

    }

    private void setLabelRep(int y) throws IOException {

        Font font = CommonUtils.getFont();
        int width = WorkPanel.width-90;
        this.setIcon(ImageUtils.common);
        this.setVerticalTextPosition(JLabel.CENTER);
        this.setHorizontalTextPosition(JLabel.CENTER);
        this.setFont(font);
        this.setForeground(Color.WHITE);
        this.setBounds(5,y,WorkPanel.width-20,30);
        keyField.setFont(CommonUtils.getFont());
        keyField.setForeground(Color.WHITE);
        keyField.setBounds(40,1,width/2-10,28);
        keyField.setOpaque(false);
        keyField.setText(key);
        valueField.setFont(CommonUtils.getFont());
        valueField.setForeground(Color.WHITE);
        valueField.setBounds(40 + width/2,1,width/2-10,28);
        valueField.setOpaque(false);
        valueField.setText(value);
        setKeyUneditable();
        setValueUneditable();
        this.add(keyField);
        this.add(valueField);
        this.add(new BarLabel(32,0));
        this.add(new BarLabel(32 + width/2,0));
        this.add(new BarLabel(32 + width,0));

    }

    public void setY(int y){
        this.setBounds(5,y,WorkPanel.width-20,30);
    }

    public void setUnEditable(){
        this.remove(yes);
        this.remove(no);
    }


    public void refresh(String name) throws IOException {
//        recordPanel.refresh(name);
    }

    public void setKeyUneditable(){
        keyField.setEditable(false);
    }

    public void setValueUneditable(){
        valueField.setEditable(false);
    }

    public void setDescriptionUneditable(){
        descriptionField.setEditable(false);
    }

    /**
     * hold监听器
     */
    private class deleteParam implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            commonPanel.removeParamLabel(symbol);
        }
    }

    /**
     * hold监听器
     */
    private class statusListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            isChosen = !isChosen;
        }
    }

}

